﻿using System.Configuration;
using System.Linq;
using MyWebApp.Integration.AllegroWebApi;

namespace MyWebApp.Integration
{
    public class AllegroSearchService 
    {
        private readonly servicePortClient _servicePortClient;

        private string ApiLogin
        {
            get { return ConfigurationManager.AppSettings["login"]; }
        }

        private string ApiPassword
        {
            get { return ConfigurationManager.AppSettings["password"]; }
        }

        private string ApiKey
        {
            get { return ConfigurationManager.AppSettings["apiKey"]; }
        }

        private int ApiCountryId
        {
            get { return 1; }
        }

        private long ApiVersionKey 
        {
            get
            {
                long versionKey;
                _servicePortClient.doQuerySysStatus(1, ApiCountryId, ApiKey, out versionKey);
               
                return versionKey;
            }
        }

        private string _sessionHandle;
        private string SessionHandle
        {
            get
            {
                if (string.IsNullOrEmpty(_sessionHandle))
                {
                    long userId;
                    long serverTime;
                    _sessionHandle = _servicePortClient.doLogin(ApiLogin, ApiPassword, ApiCountryId, ApiKey, ApiVersionKey, out userId, out serverTime);
                }

                return _sessionHandle;
            }
        }

        public AllegroSearchService()
        {
            _servicePortClient = new servicePortClient();
        }

        public SearchResponseType[] DoSearch(string term, int categoryId, int stateId, out int numberOfItemsFound)
        {

            int searchCountFeatured;
            SearchResponseType[] searchArray;
            CategoriesStruct[] categories;
            string[] excludedWords;

            var result = _servicePortClient.doSearch
                    (
                        SessionHandle,
                        new SearchOptType
                        {
                            //1 - bierz pod uwagę którekolwiek z wpisanych słów,
                            //2 - szukaj również w opisach.
                            //65536 - szukaj tylko w przedmiotach z wartością "Używany" w parametrze "Stan"
                            //16 - szukaj w zakończonych
                            searchOptions = 65538 ,
                            searchOptionsSpecified = true,
                            searchState = stateId,
                            searchStateSpecified = stateId != 0,
                            searchCategory = categoryId,
                            searchCategorySpecified = true,
                            searchString = term,
                            searchLimit = 100,
                            searchLimitSpecified = true
                        }, 
                        out searchCountFeatured,
                        out searchArray,
                        out excludedWords, 
                        out categories
                    );

            numberOfItemsFound = searchArray.Count();
            return searchArray;
        }

        public ItemInfoStruct[] GetItemInfos(long[] ids)
        {

            long[] arrayItemsNotFound;
            long[] arrayItemsAdminKilled;
            var result = _servicePortClient.doGetItemsInfo(SessionHandle, ids, 1, 1, 0, 0, 0, 0, out arrayItemsNotFound,
                out arrayItemsAdminKilled);

            return result;
        }

        public CatInfoType[] GetAllCategories()
        {
            string verStr;
            long verKey;
            var categories = _servicePortClient.doGetCatsData(ApiCountryId, ApiVersionKey, ApiKey, out verKey, out verStr);
            
            return categories;
        }
    }
}
